/*
 * @Author: wenqingwu
 * @Date: 2022-03-10 12:53:17
 * @LastEditTime: 2022-04-01 11:19:24
 * @LastEditors: WenQingwu
 * @Description: 响应规范
 * @FilePath: \doctormanage\routes\msgJson.js
 */
module.exports = {

  /** 
  * 参数为空时
  * @param {String} name 操作名称,required
  * @returns  {Object}
  */
  paramIsNull(name) {
    return { code: 40001, msg: name + '参数不能为空值' }
  },

  /**
   * 请求成功时
   * @param {String} name 操作名称,required
   * @param {any} data 
   * @returns {Object}
   */
  requestSuccess(name, data = {}) {
    return { code: 0, msg: name + '操作成功', data }
  },

  /**
   * 请求失败时，主要是服务器问题
   * @param {String} name 操作名称,required
   * @param {String} errInfo 
   * @returns  {Object}
   */
  requestFail(name, errInfo) {
    return { code: 500, msg: name + '操作失败！原因：' + errInfo, data: {} }
  },

  /**
   * 参数必传
   * @param {String} name 操作名称,required
   * @returns  {Object}
   */
  paramIsRequired(name) {
    return { code: 40001, msg: name + '参数 required' }
  },

  paramOfType(name,errInfo=''){
    return {code :415, msg:name + '类型不正确.'+errInfo}
  },

  /**
   * 检查是否有这个参数，参数是否传值
   * @method 说明: 该方法不适合用于查询\修改功能. 适合用于增加,删除,参数少时,特别是参数检查很多时. 
   * @param {Object} obj 参数对象,required
   * @param {Array} propertyArr 需要检查的参数列表,required
   * @requires {Object}
   */
  checkParam(obj, propertyArr) {
    const arr = propertyArr;
    let result = null;
    for (let i = 0; i < arr.length; i++) {
      if (obj[arr[i]] === undefined) {
        result = this.paramIsRequired(arr[i]);
        break;
      }
      console.log('params check type:',typeof obj[arr[i]])
      if (typeof obj[arr[i]] === 'string' && obj[arr[i]].trim().length === 0) {
        result = this.paramIsNull(arr[i]);
        break;
      }
    }
    console.log("参数检查结果:", result);
    return result;
  }
}